<?php
	session_start();
	ini_set( "display_errors", 0);
	// Informations de connexion à la bd.
	$host = "localhost";
	$port = 5432;
	$dbname = "demo";
	$user = "postgres";
	$pass = "pass";
	$db = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$pass");

	if (!isset($_GET["page"]))
	{
		$_GET["page"]="accueil";
	}
	switch ($_GET["page"])
	{ 
		case "formADD": // dans le cas où $note vaut 0
		$_GET["page"]="formADD";
		break;

		case "formEDIT": // dans le cas où $note vaut 0
		$_GET["page"]="formEDIT";
		break;

		case "formREM": // dans le cas où $note vaut 0
		$_GET["page"]="formREM";
		break;

		default:
		$_GET["page"]="accueil";
	}
	if (isset($_POST['theso']))
	{
		if (!isset($_SESSION['theso']))
		{
			$_SESSION['theso'] = $_POST['theso'];
		}
		else if($_POST['theso'] != $_SESSION['theso'])
		{
			$_SESSION['theso'] = $_POST['theso'];
		}
	}
	else
	{
		if (!isset($_SESSION['theso']))
		{
			$result = pg_query($db, "SELECT * FROM Thesaurus LIMIT 1;");
			while ($data= pg_fetch_object($result))
			{
				$_SESSION['theso'] = $data->nom;
			}
		}
	}

	
?>

<html>
    <head>
        <meta charset="utf-8" />
        <title>Thésaurus</title>
	<link rel="stylesheet" href="style.css" />
	<script type="text/javascript" src="javascript/list.js">  </script>
	<script type="text/javascript" src="javascript/jquery143.js"></script> 
	<script type="text/javascript" src="javascript/jquery.editable-select.pack.js">  </script>
	<script type="text/javascript" src="javascript/jquery.editable-select.js">  </script>
	<script type="text/javascript"> 
		  $(function() {
		  $('.editable-select').editableSelect(
		    {
		      bg_iframe: true,
		      case_sensitive: false, // If set to true, the user has to type in an exact
				             // match for the item to get highlighted
		      items_then_scroll: 10 // If there are more than 10 items, display a scrollbar
		    }
		  );
		  var select = $('.editable-select:first');
		  var instances = select.editableSelectInstances();
		});
 	</script> 
    </head>

    <body>

	<header>
		<aside>
			<form id="domainForm" method="post" action="index.php">
			<label for="selectTheso">Thésaurus: </label>
			<select id="selectTheso" name="theso">
			<?php
		
				$result = pg_query($db, "SELECT * FROM Thesaurus");
				if (!$result) 
				{
					echo "An error occured.\n";
					exit;
				}
				$select = "";
				while ($data = pg_fetch_object($result))
				{
					$select = "";
					if (isset($_SESSION['theso']) && $_SESSION['theso'] == $data->nom)
					{ $select = "selected='true'"; }
					echo "<option value='" . $data->nom ."' ".$select.">".$data->nom."</option>";
				}
		
			?>	
	      		</select>

			<input type="submit" value="choisir">
		</form>
		</aside>
		<h1>Thésaurus : Administration </h1>
	</header>

	<nav>
    		<ul>
			<li> <a href="index.php"> Accueil </a> </li>
        		<li> <a href="index.php?page=formADD"> Ajout </a> </li>
			<li> <a href="index.php?page=formEDIT"> Editer </a> </li>
			<li> <a href="index.php?page=formREM"> Supprimer </a> </li>
    		</ul>
    		
	</nav>

	<section id="articles">
	<?php 
		if ($_GET["page"] == "accueil")
		{
			include('accueil.php'); 
		}
		else if ($_GET["page"] == "formADD")
		{
			include('formulaireAdminCreate.php'); 
		}
		else if ($_GET["page"] == "formEDIT")
		{
			include('formulaireAdminModif.php'); 
		}
		else if ($_GET["page"] == "formREM")
		{
			include('formulaireAdminRem.php'); 
		}
	?>
	<section>
    </body>
</html>

<?php

	/*<form method="post" action="cible.php">
				<input id="search" type="text" class="textInput"/>
				<input id="sendSearchButton" type="image" src="images/search.png"/>
			</form>*/

?>
